马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Python 爬取淘帖里的帖子标题
from requests import get
from bs4 import BeautifulSoup as BS
def get_Tz(res):
soup = BS(res.text, "html.parser")
targ = soup.find_all("a",class_="xst", target="_blank")
for each in targ:
print(each.text)
#print(each['title'])
print(each['href'])
def get_Tz_write(res, f):
soup = BS(res.text, "html.parser")
targ = soup.find_all("a",class_="xst", target="_blank")
for each in targ:
f.write(each.text)
f.write(each['href'])
f.write('\n')
def main():
n = int(input("请输入要下载的次数:"))
f = open("淘帖.txt", "w")
for i in range(1,n):
url = "https://fishc.com.cn/forum.php?mod=collection&action=view&ctid=%d" % i
res = get(url)
get_Tz_write(res, f)
f.close()
if __name__ == "__main__":
main()
有时候会报错,是正常现象,重新运行就好
不要爬太多哦~ |