import re
import time
from urllib import parse
import urllib
import requests
def updatepostinfo(startid,endid):
for num in range(int(startid),int(endid)+1):
time.sleep(2)
print(num)
posturl="http:网站/detailnew.php?id="+str(num)
postres=urllib.request.urlopen(posturl)
postres=postres.read().decode()
print(postres)
if "帖子不存在" in postres:
continue
elif "错误,帖子" in postres:
continue
elif "该论坛不存在" in postres:
continue
elif "访问错误" in postres:
continue
elif "此帖审核中" in postres:
continue
elif "分版权限" in postres:
continue
else:
posttitle=re.findall(r'<card id=\"main\" title=\"(.+?)\">',str(postres))
postcontent=re.findall(r'name=\"content\" value=\"(.+?)\" />',str(postres))
postcontent=postcontent[0]
postauthid=re.findall(r"userid=(\d+)",str(postres))
postauthid=postauthid[0]
postforumid=re.findall(r"bid=(\d+)",str(postres))
postforumid=postforumid[0]
#下面存入对应字段到数据库
if __name__ == '__main__':
updatepostinfo(1,292812334)

此博客展示了一段Python代码,通过`urllib`和`requests`库批量获取帖子信息。代码从指定的起始ID到结束ID遍历,访问帖子URL,读取页面内容。若页面包含特定错误信息则跳过,否则使用正则表达式提取帖子标题、内容、作者ID和论坛ID,后续可存入数据库。
1万+

被折叠的 条评论
为什么被折叠?



