Article Directory
1. Crawl the original page
Website link used: https://ipchaxun.com/ .
The page is as follows:
2. Programming ideas
Copy the IP address you want to query to variable a. By observing the changes of the URL of the website before and after the query, a URL link that can be queried after submitting the IP address is constructed based on the rules. After the content is obtained, the fragment output can query the IP address Attribution.
3. Complete code
import requests
def getHTMLText(url):
try:
kv={
'user-agent':'Mozilla/5.0'}
r=requests.get(url,headers=kv)
r.raise_for_status()
r.encoding=r.apparent_encoding
print(r.text[2000:3000])#分片查看相应字节
except:
print("爬取失败")
def main():
a=input("请输入要查询的IP地址:")
url='https://ipchaxun.com/'+a
getHTMLText(url)
main()
At the end of this article, please point out any errors~
Quote from
中国大学MOOC Python网络爬虫与信息提取
https://www.icourse163.org/course/BIT-1001870001