小编给大家分享一下如何使用python爬取当当网所有数据分析书籍信息,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
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爬取当当网所有数据分析书籍信息”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4607696/blog/4996378