我创建了一个脚本来显示网站上的所有空缺。这很好用,将通过SSH垂直打印列表。
但是,我现在需要做的是将此输出保存为无序列表并将其保存到.html页面。
我使用的脚本是:
from lxml import html
import requests
page = requests.get('https://www.fasthosts.co.uk/careers/current-vacancies').text
tree = html.fromstring(page.content)
Vacancies = tree.xpath('//h1[@class="featuredvacancy__title featuredvacancy__title--invert grid-16 alpha"]/text()')
print Vacancies
这会将输出打印到屏幕。
然而我的另一个剧本:
import requests
from bs4 import BeautifulSoup
url = 'https://www.fasthosts.co.uk/careers/current-vacancies'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(response.content, 'html.parser')
output = soup.find ('//h1[@class="featuredvacancy__title featuredvacancy__title--invert grid-16 alpha"]/text()')
text, link = output.text, output.get('vacancy.html')
返回此错误:
文件" test2.py",第11行,在中 text,link = output.text,output.get(' vacancy.html') AttributeError:' NoneType'对象没有属性' text'
我现在已经解决了使用以下脚本将输出保存到.html文件的问题:
from lxml import html
import requests
import urllib2
page = requests.get('https://www.fasthosts.co.uk/careers/current-vacancies')
content = html.fromstring(page.content)
Vacancies = content.xpath('//h1[@class="featuredvacancy__title featuredvacancy__title--invert grid-16 alpha"]/text()')
f = open('vacancy.html', 'w')
f.write(str(Vacancies))
f.close
答案 0 :(得分:0)
使用以下脚本将输出保存到.html
文件解决了问题:
from lxml import html
import requests
import urllib2
page = requests.get('https://www.fasthosts.co.uk/careers/current-vacancies')
content = html.fromstring(page.content)
Vacancies = content.xpath('//h1[@class="featuredvacancy__title featuredvacancy__title--invert grid-16 alpha"]/text()')
f = open('vacancy.html', 'w')
f.write(str(Vacancies))
f.close
基于OP对其帖子的修改(可能受@user3080953的评论影响。