You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
717 B

import urllib.request
import urllib.parse
from lxml import etree
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
url='https://www.news.cn/'
headers = {
'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
}
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
tree = etree.HTML(content)
news_names=tree.xpath("//div[@class='depth-cont']//a/text()")
news_names_list=news_names[:10]
for news_name in news_names_list:
with open('ºúÎĺÆ.txt','a',encoding='utf-8') as f:
f.write(news_name+'\n')