当前位置:网站首页>50 lines of code to crawl TOP500 books and import TXT documents
50 lines of code to crawl TOP500 books and import TXT documents
2022-06-26 18:17:00 【Little fox dreams of going to fairy tale town】
50 Line code crawl Top500 Book Import TXT file
import re # Regular expressions , Text extraction
import requests
import json
def main(page):
# Claim to crawl the URL
baseurl = "http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-" + str(page)
# Crawling the web content
datalist = getData(baseurl)
# Save web data
savepath = "Top500_book.txt"
saveData(datalist,savepath)
# Get data
def getData(baseurl):
html = askURL(baseurl)
datalist = parse_result(html)
return datalist
# Parse the source code
def parse_result(html):
pattern = re.compile('<li>.*?list_num.*?(\d+).</div>.*?<img src="(.*?)".*?class="name".*?title="(.*?)">.*?class="star">.*?class="tuijian">(.*?)</span>.*?class="publisher_info">.*?target="_blank">(.*?)</a>.*?class="biaosheng">.*?<span>(.*?)</span></div>.*?<p><span\sclass="price_n">¥(.*?)</span>.*?</li>',re.S)
items = re.findall(pattern,html)
for item in items:
yield {
'range': item[0],
'iamge': item[1],
'title': item[2],
'recommend': item[3],
'author': item[4],
'times': item[5],
'price': item[6]
}
# Get web source
def askURL(url):
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
except requests.RequestException:
return None
# Save data to txt Text document
def saveData(datalst,savepath):
print("save....")
for item in datalst:
with open(savepath, 'a', encoding='UTF-8') as f:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
f.close()
if __name__ == '__main__':
#for Loop to turn the page
for i in range(1,26):
main(i)
【 Running results 】
边栏推荐
猜你喜欢
随机推荐
Paging query and join Association query optimization
Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
Comparing the size relationship between two objects turns out to be so fancy
Procedure steps for burning a disc
MySQL download and configuration MySQL remote control
Case study of row lock and isolation level
Map和List<Map>转相应的对象
Bayesian network explanation
How to open a stock account? Is it safe to open an account online now?
The cross compilation environment appears So link file not found problem
输入n个整数,输出出现次数大于等于数组长度一半的数
行锁与隔离级别案例分析
Digital signature standard (DSS)
(必须掌握的多线程知识点)认识线程,创建线程,使用Thread的常见方法及属性,以及线程的状态和状态转移的意义
Detailed explanation of MySQL mvcc mechanism
CD-CompactDisk
Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems
贝叶斯网络详解
Temporarily turn off MySQL cache
预编译处理指令中的条件编译









