为什么我的这段代码content没有值:from lxml import etree import requests urls=[] for i in range(0,5,1): i=i*20 url = 'https://movie.douban.com/review/best/?start={}'.format(i) urls.append(url) import requests from lxml import etree herders = { "Content-Type":"text/html; charset=utf-8", "uesr-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67" } detail_urls = [] for url in urls: # 发送请求 response= requests.get(url,headers=herders) # 编码转码 content = response.content.decode('utf8') # 解析html字符串 html =etree.HTML(content) # 利用xpath提取每个电影影评的url detail_url = html.xpath('//h2/a/@href') detail_urls.append(detail_url) print(detail_urls) break
时间: 2024-04-10 18:30:53 浏览: 24
到sqrt(n)。在每次迭代中,我们检查 `$n` 是否可以整除当前的迭代变量 `$i`。如果可以整除,则将 `$i` 加入到数组中,并将 `$n` 更新为 `$n` 除以 `$i` 的结果。
最后,如果 `$n` 大于2,则说明 `$n` 本身也是一个质因子,我们将其加入到数组中。
最后,我们使用 `echo` 语句输出找到的质因子。
如果你运行这段代码,并将 `$n` 设置为36,它将输出:
```
正整数 36 的质因子为: 2 2 3 3
```
希望这能解决你的问题!如果你还有其他疑问,请随时提问。
相关问题
import requests from lxml import etree urls = ['https://www.xxddxs.com/book/{}.html'.format(i) for i in range(1, 20)] path = r'D:\py\venv\Scripts\xiaoshuo' def get_text(url: object) -> object: r = requests.get(url) r.encoding = 'utf-8' selector = etree.HTML(r.text) # Get the book title from the page title = selector.xpath('//div[@class="bookname"]/h1/text()') # If no title found, print an error message and return early if len(title) == 0: print(f"No title found for {url}") return # Get the text of the chapter text = selector.xpath('//div[@id="content"]/text()') # Print the text to standard output print(text) # Write the text to a file in the specified directory with open(path + "\\" + title[0], 'w', encoding='utf-8') as f: for i in text: f.write(i) if __name__ == '__main__': for url in urls: get_text(url) 帮我修改并可以爬取到标题
Sure! 以下是修改后的代码:
```python
import requests
from lxml import etree
urls = ['https://www.xxddxs.com/book/{}.html'.format(i) for i in range(1, 20)]
path = r'D:\py\venv\Scripts\xiaoshuo'
def get_text(url):
r = requests.get(url)
r.encoding = 'utf-8'
selector = etree.HTML(r.text)
# Get the book title from the page
title = selector.xpath('//div[@class="bookname"]/h1/text()')
# If no title found, print an error message and return early
if len(title) == 0:
print(f"No title found for {url}")
return
# Get the text of the chapter
text = selector.xpath('//div[@id="content"]/text()')
# Print the text to standard output
print(text)
# Write the text to a file in the specified directory
with open(path + "\\" + title[0].strip() + ".txt", 'w', encoding='utf-8') as f:
for i in text:
f.write(i)
if __name__ == '__main__':
for url in urls:
get_text(url)
```
在原有代码的基础上,我在 `open()` 方法中添加了 `.strip()`,以去除标题中的空白字符。此外,我还为文件添加了 `.txt` 扩展名,以方便后续的操作。
import requests from lxml import etree #发起请求 headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36 Edg/113.0.1774.57'} def get_fist_teat(list): try: return list[0].strip() except: return " " urls = ['https://movie.douban.com/top250?start={}&filter='.format(str(i*25)) for i in range(10)] cont = 1 for url in urls: res = requests.get(url=url,headers=headers) html = etree.HTML(res.text) lis = html.xpath('//*[@id="content"]/div/div[1]/ol/li') #解析数据 for li in lis: title = get_fist_teat(li.xpath('./div/div[2]/div[1]/a/span[1]/text()')) scr = get_fist_teat(li.xpath('./div/div[2]/div[1]/a/@href')) #获取合副属性+@href dictor = get_fist_teat(li.xpath('./div/div[2]/div[2]/p[1]/text()')) scort = get_fist_teat(li.xpath('./div/div[2]/div[2]/div/span[2]/text()')) commen = get_fist_teat(li.xpath('./div/div[2]/div[2]/div/span[4]/text()')) print(cont,title,scr,dictor,scort,commen) cont += 1如何实现可视化,比如加上柱状图
要将数据可视化为柱状图,可以使用 Python 中的 Matplotlib 库。以下是修改后的代码示例,可以将每部电影的评分作为柱状图进行可视化:
```
import requests
from lxml import etree
import matplotlib.pyplot as plt
# 发起请求
headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36 Edg/113.0.1774.57'}
def get_fist_teat(list):
try:
return list[0].strip()
except:
return " "
urls = ['https://movie.douban.com/top250?start={}&filter='.format(str(i*25)) for i in range(10)]
scores = [] # 保存电影评分
cont = 1
for url in urls:
res = requests.get(url=url,headers=headers)
html = etree.HTML(res.text)
lis = html.xpath('//*[@id="content"]/div/div[1]/ol/li')
# 解析数据
for li in lis:
title = get_fist_teat(li.xpath('./div/div[2]/div[1]/a/span[1]/text()'))
scr = get_fist_teat(li.xpath('./div/div[2]/div[1]/a/@href')) #获取合副属性+@href
dictor = get_fist_teat(li.xpath('./div/div[2]/div[2]/p[1]/text()'))
scort = get_fist_teat(li.xpath('./div/div[2]/div[2]/div/span[2]/text()'))
commen = get_fist_teat(li.xpath('./div/div[2]/div[2]/div/span[4]/text()'))
# 保存评分数据
scores.append(float(scort))
print(cont,title,scr,dictor,scort,commen)
cont += 1
# 绘制柱状图
plt.bar(range(len(scores)), scores)
plt.title('Top 250 Movies on Douban')
plt.xlabel('Movie Rank')
plt.ylabel('Movie Score')
plt.show()
```
在上面的代码中,我们首先创建了一个空列表 scores,用于保存每部电影的评分。在解析每部电影的数据时,我们将电影的评分转换为浮点数,并将其添加到 scores 列表中。最后,我们使用 Matplotlib 库的 `bar` 函数将 scores 列表中的评分数据绘制成柱状图,并添加了一些必要的标签和标题。最后使用 `show` 函数显示图形。