Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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解析HTML?_Python_Html Parsing - Fatal编程技术网

Python,如何从URL解析HTML?

Python,如何从URL解析HTML?,python,html-parsing,Python,Html Parsing,我有Python代码,可以解析包含HTML代码的字符串变量中的数据 我需要从URL获取HTML,然后解析这些数据的代码 工作代码(解析HTML): 我没有收到任何错误,它没有打印任何内容。 非常感谢您的帮助。然后在调试过程中使用print(html\u text)查看您得到了什么;) 当您打印它时,您会看到它与页面源不同(在Chrome或其他webbrowser中查看它并转到url)。您还可以看到,当您在浏览器中转到该页面时,该页面正在加载一段时间 因此,您需要等待它加载类似的内容 为了演示一点

我有Python代码,可以解析包含HTML代码的字符串变量中的数据

我需要从URL获取HTML,然后解析这些数据的代码

工作代码(解析HTML):

我没有收到任何错误,它没有打印任何内容。
非常感谢您的帮助。

然后在调试过程中使用
print(html\u text)
查看您得到了什么;)

当您打印它时,您会看到它与页面源不同(在Chrome或其他webbrowser中查看它并转到url)。您还可以看到,当您在浏览器中转到该页面时,该页面正在加载一段时间

因此,您需要等待它加载类似的内容

为了演示一点Selenium,我加载了您的页面,并单击了一些具有定义类的内容,这些类在一段时间后加载:

# you will have to install (Chrome), or another browser driver
from selenium.webdriver import Chrome

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

driver = Chrome(r'C:\Program Files\chromedriver.exe')  # I have (Chrome) installed here

driver.get("https://www.pinterest.com/search/pins/?q=skin%20care")
feeling_lucky_btn = WebDriverWait(driver, 3).until(  # waiting for loading
    EC.presence_of_element_located(
    (By.CLASS_NAME, 'GrowthUnauthPinImage__Image')))  # identifiing element by class name
feeling_lucky_btn.click()

谢谢你的回答,是的,但是我想要正确的结果,代码产生的结果,只是打印出长HTML代码不会解决我的问题,除非你有关于如何使用它的提示。如果
HTML\u text
data
(你的示例)相同,并且你的示例有效,那么你尝试的也同样有效,对吗?谢谢你的回复,我正在查看打印结果,很长的HTML代码,没有假定的代码,我现在很困惑。@Brambor这在
请求中是否可能?我认为他需要使用
selenium
?不是吗?我看到他的汤里只有一个主分量。没有其他内容。@Dave99我在回答中添加了硒的演示;)。你真的确定
html\u text
中有你想要的文本吗?也就是说,它包含您想要的内容,而不是登录页面?
import requests
from bs4 import BeautifulSoup

vgm_url = 'https://www.pinterest.com/search/pins/?q=skin%20care'
html_text = requests.get(vgm_url).text
soup = BeautifulSoup(html_text, 'html.parser')
a = soup.select('div.Eqh.F6l.Jea.k1A.zI7.iyn.Hsu a')
for a in soup.select('div.Eqh.F6l.Jea.k1A.zI7.iyn.Hsu a'):
    print(a['title'])
# you will have to install (Chrome), or another browser driver
from selenium.webdriver import Chrome

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

driver = Chrome(r'C:\Program Files\chromedriver.exe')  # I have (Chrome) installed here

driver.get("https://www.pinterest.com/search/pins/?q=skin%20care")
feeling_lucky_btn = WebDriverWait(driver, 3).until(  # waiting for loading
    EC.presence_of_element_located(
    (By.CLASS_NAME, 'GrowthUnauthPinImage__Image')))  # identifiing element by class name
feeling_lucky_btn.click()