Python爬虫学习第一天

###调用requests库,获取网页源代码
import requests
url = “http://www.baidu.com”
r = requests.get(url)
r.raise_for_status() #产生异常信息
r.encoding = r.apparent.encoding #修改编码
print(r.text)

###爬取京东中的一个页面
import requests
url = “https://item.jd.com/100000177760.html”
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.text)
expect:
print(“爬取失败”)

###百度360搜索关键词提交
import requests
try:
kv = {‘wd’:‘Python’}
r = requests.get(“https://www.baidu.com/s”,params=kv)
print(r.request.url) #打印搜索后的url
r.raise_for_status()
print(len(r.text))
expect:
print(“爬取失败”)

###网络图片的爬取和存储(url后带.jpg)
import requests
import os
url = “https://wx4.sinaimg.cn/mw690/48508ff2ly1g1xwvrzmwbj23sg5og7wj.jpg”
root = “G://pics//” #保存位置
path = root + url.split(’/’)[-1] #保存的图片名称
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requessts.get(url)
with open(path,‘wb’) as f:
f.write(r.content)
f.close()
print(“文件保存成功”)
else:
print(“文件已存在”)
expect:
print(“爬取失败”)

你可能感兴趣的:(Python爬虫学习第一天)