HTML Parser从上一个网页中提取

时间:2019-03-10 07:09:37

标签: python selenium findall html-parser

我有一个脚本,可以加载页面并保存来自多个容器的一堆数据ID。然后,我想打开新的URL,并将这些数据ID附加到URL的末尾。对于每个URL,我想找到所有的href并将它们与特定链接的列表进行比较,如果它们匹配,我想将该链接和其他一些详细信息保存到表中。

我设法通过附加的数据ID来打开它,但是当我尝试在新页面中搜索元素时,如果我再次尝试从汤中查找全部,它要么从已解析的第一个URL中提取它们,要么当我尝试运行另一个html.parser时,我不断收到此错误。

ResultSet对象没有属性'findAll'。您可能正在将项目列表像单个项目一样对待。当您打算致电find()时,您是否致电过find_all()?

是否无法运行另一个解析器,或者我只是做错了什么?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as soup
from selenium.webdriver.common.action_chains import ActionChains

url = "http://csgo.exchange/id/76561197999004010#x"

driver = webdriver.Firefox()

driver.get(url)
import time
time.sleep(15)
html = driver.page_source
soup = soup(html, "html.parser")

containers = soup.findAll("div",{"class":"vItem"})

print(len(containers))
data_ids = [] # Make a list to hold the data-id's

for container in containers:
    test = container.attrs["data-id"]
    data_ids.append(test) # add data-id's to the list
    print(str(test))

for id in data_ids:
    url2 = "http://csgo.exchange/item/" + id
    driver.get(url2)
    import time
    time.sleep(2)   
    soup2 = soup(html, "html.parser")
    containers2 = soup2.findAll("div",{"class":"bar"})
    print(str(containers2))

with open('scraped.txt', 'w', encoding="utf-8") as file:
    for id in data_ids:
        file.write(str(id)+'\n') # write every data-id to a new line

3 个答案:

答案 0 :(得分:0)

不确定要从每个页面中获得什么。您应该添加等待。我在每个页面的流历史记录部分(如果存在)中添加了等待以查找href的等待。应该可以说明这个想法。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = 'http://csgo.exchange/id/76561197999004010'
driver = webdriver.Chrome()
driver.get(url)
ids = [item.get_attribute('data-id') for item in WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "[data-id]")))]
results = []
baseURL = 'http://csgo.exchange/item/'
for id in ids:
    url = baseURL + id
    driver.get(url)
    try:
        flowHistory = [item.get_attribute('href') for item in WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#tab-history-flow [href]")))]
        results.append([id, flowHistory])
    except:
        print(url)

答案 1 :(得分:0)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = 'http://csgo.exchange/id/76561197999004010'
profile = webdriver.FirefoxProfile()
profile.set_preference("permissions.default.image", 2) # Block all images to load websites faster.
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
ids = [item.get_attribute('data-id') for item in WebDriverWait(driver,30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "[data-id]")))]
results = []
baseURL = 'http://csgo.exchange/item/'
for id in ids:
    url = baseURL + id
    driver.get(url)
    try:
        pros = ['http://csgo.exchange/profiles/76561198149324950']
        flowHistory = [item.get_attribute('href') for item in WebDriverWait(driver,3).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#tab-history-flow [href]")))]
        if flowHistory in pros:
            results.append([url,flowHistory])
            print(results)
    except:
        print()

答案 2 :(得分:0)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

urls = ['http://csgo.exchange/id/76561197999004010']
profile = webdriver.FirefoxProfile()
profile.set_preference("permissions.default.image", 2) # Block all images to load websites faster.
driver = webdriver.Firefox(firefox_profile=profile)
for url in urls:
    driver.get(url)
ids = [item.get_attribute('data-id') for item in WebDriverWait(driver,30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "[data-id]")))]
results = []
pros = ['http://csgo.exchange/profiles/76561198149324950', 'http://csgo.exchange/profiles/76561198152970370']
baseURL = 'http://csgo.exchange/item/'
for id in ids:
    url = baseURL + id
    driver.get(url)
try:

    flowHistory = [item.get_attribute('href') for item in WebDriverWait(driver,2).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#tab-history-flow [href]")))]
    match = []
    for string in pros:
        if string in flowHistory:
            match = string
            break

    if match:
        pass 

        results.append([url,match])
        print(results)
except:
    print()