代码如下:
# -*- coding:utf8 -*-
#导入requests库,取别名res
import requests as res
#导入bs4包,取别名bs
from bs4 import BeautifulSoup as bs
#导入数据库驱动包
import MySQLdb
#声明页面从哪开始
j = 1
#循环遍历每个页面
while j <= 111:
##获取目标网站的网页
#r代表将“”内的所有内容都默认为字符串
path = r"http://www.bengyechina.com/product/enterprise_alllist_0_0_0_" + str(j) + ".html"
#请求获取目标网页的html
doc = res.get(path)
#准备要爬取数据的列表
names = []
imgs = []
#需要bs解析器去解析网页
text = bs(doc.text,"html.parser")
#从网页中查询类标签名为plist的下标为0的所有内容
p1 = text.select(".plist")[0]
#从类标签名为plist的下标为0的html中查询标签为li的内容中的img中所有内容
img = text.select("li img")
#声明变量作为下标,并初始化
i = 0
#---------------爬取数据结束---------------
#---------------数据写入数据库----------------
#连接mysql数据库中的pachong数据库
#connect("主机名","用户