Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 爬网多个URL并在URL'中查找相同的单词出现次数;s_Python_Html_Url_Web Crawler - Fatal编程技术网

Python 爬网多个URL并在URL'中查找相同的单词出现次数;s

Python 爬网多个URL并在URL'中查找相同的单词出现次数;s,python,html,url,web-crawler,Python,Html,Url,Web Crawler,我有一个分析维基百科页面(卢旺达种族灭绝)的爬虫程序。正如你们所看到的,它非常适合一个URL,它可以找到“种族灭绝”一词的出现次数。但是我要做的是解析多个url(假设我要抓取总共8个链接)。所有维基百科链接都有相同的HTML模式,因此段落和正文变量将适用于所有url。如何在同一个python文件中读取和保存其他URL import requests import urllib.request import time from bs4 import BeautifulSoup import num

我有一个分析维基百科页面(卢旺达种族灭绝)的爬虫程序。正如你们所看到的,它非常适合一个URL,它可以找到“种族灭绝”一词的出现次数。但是我要做的是解析多个url(假设我要抓取总共8个链接)。所有维基百科链接都有相同的HTML模式,因此段落和正文变量将适用于所有url。如何在同一个python文件中读取和保存其他URL

import requests
import urllib.request
import time
from bs4 import BeautifulSoup
import numpy as np
import pandas as pd
from urllib.request import urlopen
from collections import Counter
import re
def my_url_function(url):


    URL = 'https://en.wikipedia.org/wiki/Rwandan_genocide'
    response = requests.get(urls)
    soup = BeautifulSoup(response.text, 'html.parser')

    paragraph = soup.find('div',{'id':'mw-content-text'}, {'class':'mw-content-ltr'}).tbody

    #print(paragraph)


    body_text = soup.findAll("div", class_="mw-parser-output")[0].findAll('p')
    body_text_big = ""
    for i in body_text:
        body_text_big = body_text_big +i.text

    print(body_text_big)

    text_file = open("Output.txt", "w")

    text_file.write(body_text_big)

    text_file.close()


    occurrences = body_text_big.count("genocide")
    print('Number of occurrences of the word :', occurrences)

# read URLs into list `urls`
with open("urls.txt", "r") as urlsFile:

    urls = urlsFile.read().splitlines() 

print(urls)

首先,确保您在任何地方都使用相同的变量名—其中有
url
url
url
。对于列表中的多个URL,使用
For
循环在列表上迭代,将每个URL传递给函数。请确保您正在以追加模式写入结果文本文件,这样您就不会每次都删除数据。请不要人为地“标记”标题,例如使用
(Python)
。堆栈溢出有真正的标记,它们已经足够了。Hello@MattDMo,如何为每个url创建单独的附加函数?您可以在url之后命名输出文本文件
filename=url.split(“/”[-1]+.txt“
)。