小编给大家分享一下如何使用python爬取当当网所有数据分析书籍信息,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联主营金昌网站建设的网络公司,主营网站建设方案,成都app软件开发,金昌h5微信小程序搭建,金昌网站营销推广欢迎金昌等地区企业咨询
urls = ['http://search.dangdang.com/?key=%CA%FD%BE%DD%B7%D6%CE%F6&act=input&page_index={}'.format(i) for i in range(1,101)]
html=requests.get(url,headers=headers)
# html.encoding = "utf-8"
# print('第一层调用是否返回正常:',html)
html.encoding = html.apparent_encoding # 将乱码进行编码
selector=etree.HTML(html.text)
# print(selector)
datas=selector.xpath('//div[@class="con shoplist"]')
# print(datas)
for data in datas:
Classs = data.xpath('div/ul/li/@class') #line1-line60
IDDs = data.xpath('div/ul/li/@id') #id
titles = data.xpath('div/ul/li/a/@title') #标题
prices = data.xpath('div/ul/li/p[3]/span[1]/text()') #书籍价格
source_prices = data.xpath('div/ul/li/p[3]/span[2]/text()') #书籍原价
discounts = data.xpath('div/ul/li/p[3]/span[3]/text()') #书籍折扣
# dian_prices = data.xpath('div/ul/li/p[3]/a[2]/i/text()') #电子书价格
authors = data.xpath('div/ul/li/p[5]/span[1]/a[1]/@title') #作者
publish_times = data.xpath('div/ul/li/p[5]/span[2]/text()') #出版时间
publishs = data.xpath('div/ul/li/p[5]/span[3]/a/text()') #出版社
comments = data.xpath('div/ul/li/p[4]/a/text()') #书籍评论量
urls=data.xpath('div/ul/li/a/@href')
db = pyMySQL.connect(host='localhost', user='root', passwd='库密码', db='库名称:Learn_data', port=3306, charset='utf8')print("数据库连接")cursor = db.cursor()cursor.execute("DROP TABLE IF EXISTS Learn_data.dangdangweb_info_detail")sql = """CREATE TABLE IF not EXISTS Learn_data.dangdangweb_info_detail ( id int auto_increment primary key, Class CHAR(100), IDD CHAR(100), title CHAR(100), price CHAR(100), source_price CHAR(100), discount CHAR(100), author CHAR(100), publish_time CHAR(100), publish CHAR(100), comment CHAR(100), dian_price CHAR(100))DEFAULT CHARSET=utf8"""cursor.execute(sql)
cursor.execute("insert into dangdangweb_info_detail (Class,IDD,title,price,source_price,discount,author,publish_time,publish,comment,dian_price)" "values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", (str(Class),str(IDD),str(title),str(price),str(source_price),str(discount),str(author) ,str(publish_time),str(publish),str(comment),str(dian_price[0])))
以上是“如何使用python爬取当当网所有数据分析书籍信息”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!