python网络爬虫与信息提取mooc------爬取实例
实例一--爬取页面
import requests
url="https//itemjd.com/2646846.html"
try:
r=requests.get(url)
r.raise_for_status()
r.encoding=r.apparent_encoding
print(r.text[:1000])
except:
print("爬取失败")正常页面爬取
实例二--爬取页面
import requests
url="https://www.amazon.cn/gp/product/B01M8L5Z3Y"
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[1000:2000])
except:
print("爬取失败")对访问用户名有限制,模拟浏览器对网站请求
实例三--爬取搜索引擎
#百度的关键词接口:http://www.baidu.com/s?wd=keyword
#360的关键词接口:http://www.so.com/s?q=keyword
import requests
keyword="python"
try:
kv={‘wd‘:keyword}
r=requests.get("http://www.baidu.com/s",params=kv)
print(r.request.url)
r.raise_for_status()
print(len(r.text))
except:
print("爬取失败")--------------------------------------------------
import requestskeyword="python"try: kv={‘q‘:keyword} r=requests.get("http://www.so.com/s",params=kv) print(r.request.url) r.raise_for_status() print(len(r.text))except: print("爬取失败")实例四--:爬取图片
import requests
import os
url="http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg"
root="F://pics//"
path=root+url.split(‘/‘)[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r=requests.get(url)
with open(path,‘wb‘) as f:
f.write(r.content)
f.close()
print("文件保存成功")
else:
print("文件已经存在")
except:
print("爬取失败")爬取并保存图片
实例五--IP地址归属地查询:
http://m.ip138.com/ip.asp?ip=ipaddress
url="http://www.ip138.com/iplookup.asp?ip="
try:
r=requests.get(url+‘202.204.80.112‘+‘&action=2‘)
r.raise_for_status()
r.encoding=r.apparent_encoding
print(r.text[-500:])
except:
print("爬取失败")有反爬了
相关推荐
fengling 2020-08-15
taiyangshenniao 2020-10-05
flycony 2020-09-23
jacktangj 2020-09-18
YENCSDN 2020-09-15
lsjweiyi 2020-09-14
digwtx 2020-09-14
拾毅者 2020-09-14
zlxcsdn 2020-09-13
weiiron 2020-08-17
amazingbo 2020-08-16
郗瑞强 2020-08-16
lispython 2020-08-16
xiesheng 2020-08-02
葫芦小金刚 2020-07-28
StevenSun空间 2020-07-26