import requestsfrom lxml import htmlimport time headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36'} url = 'http://finance.jrj.com.cn/2018/01/01200423879416.shtml'try: rep = requests.get(url, headers = headers, timeout = 5) time.sleep(1) if rep.status_code == 200: print("链接成功") con = rep.content sel = html.fromstring(con) date = sel.xpath('//div[@class="texttit_m1"]/p/text()') date = str(date).replace(']', '').replace('[', '').replace("'", '').replace(u'\u3000', '') print(date)except Exception as e: print("主页::" +url + " 链接失败, 原因::", e) pass
无法删除爬下来文本前的 u3000 已replace
慕容708150
相关分类