1 Star 0 Fork 1

ENYOOR/hellopc

forked from jackfrued/hellopc 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test9.py 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
jackfrued 提交于 2020-06-23 10:49 . 将单线程改为多线程版本
import re
from concurrent.futures.thread import ThreadPoolExecutor
import requests
pattern = re.compile('<title>(?P<foo>.*?)</title>')
def fetch_page(url):
print('Fetch:', url)
resp = requests.get(url, headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/83.0.4103.97 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;'
'q=0.9,image/webp,image/apng,*/*;'
'q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
})
print(f'{url}: {resp.status_code}')
match = pattern.search(resp.text)
print(match.group('foo'))
def main():
urls = [
'https://www.douban.com/',
'http://www.sohu.com/',
'http://www.sina.com.cn/',
'https://www.taobao.com/',
'http://jd.com/',
'https://www.python.org/',
'https://github.com/',
'https://www.linkedin.com/',
'https://leetcode.com/',
'https://pypi.org/',
]
with ThreadPoolExecutor(max_workers=8) as pool:
for url in urls:
pool.submit(fetch_page, url)
# for url in urls:
# fetch_page(url)
if __name__ == '__main__':
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/enyoor/hellopc.git
[email protected]:enyoor/hellopc.git
enyoor
hellopc
hellopc
master

搜索帮助