python爬取⽂章保存为txt,爬⾍怎么将运⾏结果保存为txt⽂
件?
import requests
import time
from bs4 import BeautifulSoup
url = "hz.58/ruanjiangong/pn{}"
def spider():
for i in range(9):
req = (url.format(str(i+1)))
soup = , "lxml")
items = soup.select("li.job_item")
for item in items:
address = item.select("div.item_con span.address")[0].text #select()返回的是list类型
name = item.select("div.item_con span.name")[0].text
salary = item.select("div.item_con p.job_salary")[0].text
if len(item.select("div.item_con div.job_wel")) > 0:
welfare = item.select("div.item_con div.job_wel")[0].text
company = item.select("div.item_con divp_name a.fl")[0].text
href = item.select("div.item_con divp_name a.fl")[0].get("href")
print("%s\t%s\t%s\t%s\t%s\t%s"%(address, name, salary, company,welfare,href))
time.sleep(2)
python怎么读取txtif __name__ == '__main__':
spider()
这个怎么保存到txt⽂件按,有会的⼤佬吗