使用beautifulsoup从网站查找特定关键字

时间:2017-11-06 11:27:17

标签: python csv beautifulsoup

你好我想从CSV文件中列出的网站中提取特定的关键词,然后列出特定网站上可用的数字字,但我的代码不能正常工作,你能帮我吗

我有一张名为dai.csv和test.CSV的excel表,dai.csv包含行中的wesites链接

from urllib.request import Request, urlopen
import bs4 as bs
import re
import csv
import pandas as pd
import requests

with open("dai.csv", "r") as scoreFile:
    scoreFileReader =csv.reader(scoreFile)
    scoreList = []
    for row in scoreFileReader:
       if len (row) != 0:
            scoreList = scoreList + [row]

scoreFile.close()


req = Request('scoreList', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = bs.BeautifulSoup(webpage,'lxml')
url = ("techyardnepal.com")

text = soup.get_text()
#word =  input("Enter the word")
word1 = input("input word")
count1 = (text.count(word1))
#word =  ("is")
print(count1)
print(url)

with open("test.csv", "r") as scoreFile:
    scoreFileReader =csv.reader(scoreFile)
    scoreList = []
    for row in scoreFileReader:
        if len (row) != 0:
            scoreList = scoreList + [row]

scoreFile.close()


with open ("test.csv","a") as scoreFile:
    scoreFileWriter =csv.writer(scoreFile)
    scoreFileWriter.writerow([url,word,count1])

scoreFile.close()

0 个答案:

没有答案