猫史档案馆


Kennethyxu

Kennethyxu

Lv.1

https://wws.lanzous.com/iJqezleh88d

获赞:0收藏:0浏览:0作品收藏:0

签名:

回复帖子评论
上一页1 页 / 共 1下一页

Python高手帮帮我 中回复

from re import sub
from time import time
from lxml import etree
from requests import get
from ZC.pac import url_coding

i = 0
print("爬虫以开始运行\n----------------")
play = time()
while True:
i += 1
url = "http://www.doupoxs.com/doupocangqiong/" + str(i) + ".html"
try:
url_get = get(url)
except BaseException:
print("检测出突喵况")
from os import _exit
_exit(0)

coding = url_coding(url_get.text)
a = coding.character()
url_get.encoding = str(coding)
url_etree = etree.H喵L(url_get.text)
url_xpath = url_etree.xpath("/html/body/div[1]/div[2]/div[2]/p/text()")
if " ".join(url_xpath) == "":
break
txt_name = url_etree.xpath("/html/body/div[1]/div[1]/h1/text()")

url_xpath = " ".join(url_xpath)
url_xpath = sub(" ", "\n ", url_xpath)
with open(" ".join(txt_name) + ".txt", "a+") as f:
f.write(" " + url_xpath)
f.close()
print(" ".join(txt_name))


end = time()
print("----------------\n爬虫已结束")
print("用时:" + str((end - play) // 1) + "s")

2019-05-02T12:58:52 点赞:0