Here is my code:
这是我的代码:
enter code here
import urllib.request
import re
from bs4 import BeautifulSoup
URLdict=dict()
class M1905:
def __init__(self,baseurl):
self.baseURL=baseurl
self.user_agent = 'Chrome/58.0(compatible;MSIE 5.5; Windows 10)'
self.headers = {'User-Agent': self.user_agent}
def getPage(self,pageNum):
url=self.baseURL+'?refresh=1321407488&page='+str(pageNum)
request=urllib.request.Request(url,headers=self.headers)
response=urllib.request.urlopen(request)
first=response.read().decode('utf-8')
BSobj = BeautifulSoup(first, "html.parser")
for a in BSobj.findAll("a", href=True):
if re.findall('/news/', a['href']):
URLdict[a['href']] = a.get_text()
for link, title in URLdict.items():
print(title, ":", link)
ContentRequest = urllib.request.Request(link, headers=self.headers)
ContentResponse = urllib.request.urlopen(ContentRequest)
ContentHTMLText = ContentResponse.read().decode('utf-8')
ContentBSobj = BeautifulSoup(ContentHTMLText, "html.parser")
Content = ContentBSobj.find("div", {"class": "mod-content"})
print(Content.get_text())
return first
baseURL='https://www.1905.com/list-p-catid-221.html'
m1905=M1905(baseURL)
m1905.getPage(1)
enter code here
im