1 Star 1 Fork 0

wangzb0/MyEs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
spider.py 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
wangzb 提交于 2019-11-29 17:15 +08:00 . Initial commit
'''
https://www.autohome.com.cn/all/
https://www.autohome.com.cn/all/2/#liststart
文章:
title
summary
a_url
img_url
tags
pip install requests
pip install beautifulsoup4
'''
import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
def work(k):
response = requests.get(url='https://www.autohome.com.cn/all/{}/#liststart'.format(k))
# print(response.headers)
# print(response.encoding)
# print(response.status_code)
# print(response.text)
response.encoding = 'GBK'
soup_obj = BeautifulSoup(response.text, 'html.parser')
div_obj = soup_obj.find(name='div', attrs={"id": "auto-channel-lazyload-article"})
# print(div_obj)
li_list = div_obj.find_all(name="li")
# count = 1
for i in li_list:
title_obj = i.find(name='h3')
if not title_obj: continue
title = title_obj.text
summary = i.find(name='p').text
a = 'https:' + i.find(name='a').get('href')
image = 'https' + i.find(name='img').get('src')
tags = a.split('/', 4)[3]
# print('start %s' % count + '*' * 100)
# print('title : ' + title)
# print('summary : ' + summary)
# print('a : ' + a)
# print('image : ' + image)
# print('tags : ' + tags)
# print('end %s' % count + '*' * 100)
print(response.url, title, tags)
print()
# count += 1
def spider():
""" 爬取汽车之家 """
t = ThreadPoolExecutor(10)
for k in range(1, 10):
t.submit(work, k)
t.shutdown()
if __name__ == '__main__':
import time
start = time.time()
spider()
print(time.time() - start)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wangzb0/MyEs.git
git@gitee.com:wangzb0/MyEs.git
wangzb0
MyEs
MyEs
master

搜索帮助