案例(二)爬虫预热
项目一:当当网店商品爬虫——爬虫类书籍为例
此案例是运用bs4库find方法对相关内容进行抓取。
-*- coding: utf-8 -*-
import requests
import csv
from bs4 import BeautifulSoup as bs
#获取网页信息
def request_dandan(url):
try:
#用户代理
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
r = requests.get(url,headers=headers)
if r.status_code == 200:
return r.text
except requests.RequestException:
return None
#存放列名
def write_item_to_file():
csv_file = open('dangdang.csv', 'w', newline='', encoding="utf-8")
writer = csv.writer(csv_file)
writer.writerow(