1 import requests
2 from bs4 import BeautifulSoup
3 import pandas as pd
4 from openpyxl import Workbook
5 import concurrent.futures
6
7 # 读取 .txt 文件中的 URL
8 with open("urls.txt", "r") as file:
9 urls = file.read().splitlines()
10
11 # 存储 URL 和 title
12 data = []
13
14 def fetch_title(url):
15 response = requests.get(url)
16 soup = BeautifulSoup(response.text, "html.parser")
17 title = soup.find("title").text
18 return (url, title)
19
20 with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
21 futures = [executor.submit(fetch_title, url) for url in urls]
22
23 for future in concurrent.futures.as_completed(futures):
24 result = future.result()
25 data.append(result)
26
27 # 将 URL 和 title 写入 Excel 文件
28 df = pd.DataFrame(data, columns=["URL", "Title"])
29
30 book = Workbook()
31 writer = pd.ExcelWriter("titles.xlsx", engine="openpyxl")
32 writer.book = book
33
34 df.to_excel(writer, index=False)
35
36 writer.save()
37   由于是最后一起写入到excel,所以单次URL获取不宜过多

最新文章

  1. 使用git把项目提交到github
  2. Jquery.Datatables dom表格定位
  3. Aspose Cells 添加数据验证(动态下拉列表验证)
  4. 在Myeclipse中添加User Library,用户自己的库
  5. [转]DIV+CSS和TABLE的区别
  6. Java编程思想学习(九) 异常处理
  7. Java Collections的排序之二
  8. Installing Python 3.5.2 from source
  9. USB学习小记-HID类键盘的报告描述符的理解
  10. ADB——keyevent命令
  11. 史上最完整的MySQL注入
  12. SpringDataJpa学习
  13. HttpAsyncClient的连接池使用
  14. 工作所用的日常 Git 命令
  15. JDBC 与 Bean Shell的使用(二)获取值,并且断言
  16. LeetCode 303. Range Sum Query - Immutable (C++)
  17. SQL SERVER ENTERPRISE EDITION-CORE VS SERVER+CAL – DEMO ON DIFFERENCES
  18. 一步一步学习IdentityServer4 (1) 概要配置说明
  19. 使用jQuery的插件qrcode生成二维码(静态+动态生成)及常见问题解决方法
  20. Spring.net(二)----初探IOC容器

热门文章

  1. SPA路由实现的基本原理
  2. Cobaltstrike —— shellcode分析(一)
  3. WebGPU 01之Hello Triangle
  4. 模拟浏览器与服务器交互(简易TomCat框架)
  5. C语言数据结构串的表示与操作的实现
  6. MySQ安装装
  7. springboot加入cloud,并注册到nacos
  8. ASP动态网页(网站)设计教程
  9. error:0308010C:digital envelope routines::unsupported
  10. phpstudy 配置域名解决index.php 过多重定向