关于源码的使用

  • 使用了request,bs4的库
  • 可以用来抓取网页中的超链接(可以设置规则)。并写入到url.txt中。
  • 我是用来抓创意工坊的mod超链接的。

只是做个笔记。方便寻找。

各路大佬也可以来指点指点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl

ssl._create_default_https_context = ssl._create_unverified_context
url = urlopen('https://steamcommunity.com/app/563560/workshop/') # 获取网页


bs = BeautifulSoup(url, 'html.parser') # 解析网页
hyperlink = bs.find_all('a') # 获取所有超链接
file = open('./url.txt', 'w')

for h in hyperlink:
hh = h.get('href')
if hh and '/sharedfiles/filedetails/' in hh and '#comments' not in hh: # 筛选链接
print(hh)
file.write(hh) # 写入到“urltxt”文件中
file.write('\n')

file.close()