- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试抓取网站上的文章。并想获取图像的src。我进行了几次尝试,但我的代码似乎无法获取所有这些 src。
我正在使用 Selenium 3.141.0 和 Python 3.7。我想获取 4 样东西:图像的 src、全文链接、标题、文章片段。我可以成功抓取其余部分,但不能成功抓取 src。我想将所有这些详细信息转储到 pandas 数据框中。
这是我要抓取的网站的代码。
<article class="w29" data-minarticles="1.00">
<a href="something.html">
<figure class="left ">
<span class="img-a is-loaded">
<img alt="stock image" title="stock image" width="245" height="135" src="pic.JPG" class="">
<noscript>
"<img src="pic.JPG" alt="stock image" title="stock image" width="245" height="135" />"
</noscript>
</span>
</figure>
<h2>
<span>
Article Title
</span>
</h2>
<p>
"Article snippet"
</p>
</a>
::after
</article>
<article class="w29" data-minarticles="1.00">
<a href="something2.html">
<figure class="left ">
<span class="img-a is-loaded">
<img alt="stock image2" title="stock image2" width="245" height="135" src="pic2.JPG" class="">
<noscript>
"<img src="pic2.JPG" alt="stock image2" title="stock image2" width="245" height="135" />"
</noscript>
</span>
</figure>
<h2>
<span>
Article Title 2
</span>
</h2>
<p>
"Article snippet 2"
</p>
</a>
</article>
<article class="w29" data-minarticles="1.00">
<a href="something3.html">
<figure class="left ">
<span class="img-a is-loaded">
<img alt="stock image3" title="stock image3" width="245" height="135" src="pic3.JPG" class="">
<noscript>
"<img src="pic3.JPG" alt="stock image3" title="stock image3" width="245" height="135" />"
</noscript>
</span>
</figure>
<h2>
<span>
Article Title 3
</span>
</h2>
<p>
"Article snippet 3"
</p>
</a>
</article>
这是我的代码:
driver.get(url)
# get sub posts
sub_posts = driver.find_elements_by_class_name("w29")
# get details
sub_list = []
for post in sub_posts:
# Get the link to the full article
sub_source = post.find_element_by_tag_name('a').get_attribute('href')
# Get the src of the post
sub_photo = post.find_element_by_tag_name('img').get_attribute('src')
# Get headline
sub_headline = post.find_element_by_tag_name('h2').text
# Get article snippet
sub_snippet = post.find_element_by_tag_name('p').text
sub_list.append([sub_photo, sub_source, sub_headline, sub_snippet])
post_df = pd.DataFrame(sub_list, columns=["photo", "source", "headline", "snippet"])
这是我尝试过的方法以及我在数据框中得到的结果,重点关注获取帖子的 src 的代码行:
尝试 1
sub_photo = post.find_element_by_tag_name('img').get_attribute('src')
尝试 1 的结果
无论出于何种原因,它删除了第一个 src,并为其余文章返回 None。
photo source headline snippet
pic.JPG something.html Article Title Article Snippet
None something2.html Article Title 2 Article Snippet 2
None something3.html Article Title 3 Article Snippet 3
尝试 2
sub_photo = post.find_element_by_xpath('//*[@id="content"]/div[6]/div[1]/div[2]/article/a/figure/span/img').get_attribute('src')
尝试 2 的结果
它抓取了第一个 src 并将相同的第一个 src 返回到其余文章。
photo source headline snippet
pic.JPG something.html Article Title Article Snippet
pic.JPG something2.html Article Title 2 Article Snippet 2
pic.JPG something3.html Article Title 3 Article Snippet 3
尝试3
sub_photo = post.find_element_by_css_selector('a>figure>span>img').get_attribute('innerHTML')
尝试3的结果
它抓取了第一个innerHTML,并为其余文章返回相同的第一个innerHTML。
photo source headline snippet
\n<img... something.html Article Title Article Snippet
\n<img... something2.html Article Title 2 Article Snippet 2
\n<img... something3.html Article Title 3 Article Snippet 3
这就是我正在寻找的:
photo source headline snippet
pic.JPG something.html Article Title Article Snippet
pic2.JPG something2.html Article Title 2 Article Snippet 2
pic3.JPG something3.html Article Title 3 Article Snippet 3
如果有人能指出我正确的方向,我将不胜感激。谢谢。
最佳答案
最初仅渲染几个图像,因此您可以将页面滚动到底部以提取所有 @src
值,也可以提取 @src
(对于可见图像)或 @data-src
(对于隐藏图像):
sub_photo = post.find_element_by_tag_name('img').get_attribute('src') or post.find_element_by_tag_name('img').get_attribute('data-src')
如果 @src
不是 None
,则返回 @src
的值;如果 @,则返回
是@data-src
的值src无
关于python - 如何在Python中使用Selenium成功获取嵌套在span标签下的所有图像的src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54604399/
我有一个模式来匹配类似的东西 ... 1036 ... 但是我不想匹配 1036 因为会抓 1036 但无论如何我不想捕获双倍跨度,因为我不需要这些数据。我需要跨度和行尾之间的数据。 我在跨度的末尾尝
如何使用正则表达式来匹配该字符串: regularexpression . 如何交换第一个跨度的内容和第二个跨度的内容。 我认为可以使用jquery源码。 最佳答案 请务必阅读 RegEx match
我正在测试一个指令,该指令会在 200 个字符后 chop 文本。它改变了这一点: 对此: long text long text long text long text long
我有代码将这个字符串解析成一个字符数组: var textArray = Regex.Replace(text, @"]*|/)?>", String.Empty).Trim().ToCharA
我正在尝试使用 BeautifulSoup 提取包含在 id="titleDescription"范围内的字符串。 Customer Choice Award Winner
Hello
拆分为 Hello如何分割Hello至 Hello使用javascript var text = "Hello"; 记住:我不知道什么包含 , 我不知道 有没有属性 我找到了答案! var patt=/^(.*)$/i
我有一个数组列表 ArrayList al = new ArrayList(); al.add("tree good has"); al.add("ok go by"); al.add
我有一个使用 span 的 html 文件关键字以两种不同的方式。 第一个在第二个定义中 button.groovyButton span这里: button.groovyButton { b
仍在尝试让新站点的导航控件按照我想要的方式工作。 我将我的问题简化为这段代码: Test span { display: inline-block; heigh
This is 城市。它因 而闻名。
”我是编码新手 在下面的 pgm 中,任何人都可以帮我找出为什么 ng-bind 不起作用吗? 提前致谢。 Angular js Welocme!Please enter valu
我必须在下面的 html 代码中提取文本内容以进行 python 网络抓取,问题是类参数,所有三个变量都具有相同的类参数,所以我尝试使用 arial-label,但它不起作用。 2, 3 Proper
如何填充一组 带有一个带有循环的单词数组? 如果数组包含[ "one", "two", "three" ]并被称为“wordarray” 然后我想填充跨度,使其看起来像 one two
如果我们有 div 并且没有跨度,即一个跨度中的跨度,并且每个跨度都有一种颜色样式,如何使用 css 或 jquery 覆盖特定样式[颜色]..。请帮助我 最佳答案 你的意思是这样的吗? red bl
我以为下面的选择器只会匹配example b。有人可以向我解释一下 CSS 路径的工作原理吗? body div span a{ background:#000; color:#fff
我正在尝试获取内可用的文本元素。我已经使用innerHTML来检索内部文本,但很少元素内部文本放置在 内元素。 在检索文本值时,将其获取为 sample text作为输出字符串。谁能帮我删除 在输出
我应该加上“n” (显示“n”flaticon-icons)到一个div(我的页面是.php) 这是 div 这是 div_icon CSS 类 @media (max-width: 600px
无法获取“表格”中的跨度文本,谢谢! from bs4 import BeautifulSoup import urllib2 url1 = "url" content1 = urllib2.urlo
这个问题在这里已经有了答案: My inline-block elements are not lining up properly (5 个答案) 关闭 8 年前。 HTML $ 400 这会在同
我正在尽力在图像中插入 span 标签标题。 两者都在段落和 span 标签内。 如何让 lorem ipsum 文本作为标题出现在图像的底部?? Lorem ipsum dolor
我有以下 HTML 文件。 "| Testing" 我要打印 "| Testing" , 而不是打印这个打印 "| Testing" .所有这
我是一名优秀的程序员,十分优秀!